using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Configurations;
using System.Xml.Serialization;
namespace Pixysoft.Framework.Reports.Core
{
    class ReportRowSetting : IReportRowSetting
    {
        private IReportRowSetting parent;//ignore

        private string settingCode;
        private Int32 rowIndex;
        private ExpressionType expressionType;
        private String headerName;
        private String columnName;
        private List<ReportRowSetting> childrenSettings = new List<ReportRowSetting>();


        public Int32 RowIndex
        {
            get
            {
                return rowIndex;
            }
            set
            {
                rowIndex = value;
            }
        }

        public string SettingCode
        {
            get { return settingCode; }
            set { settingCode = value; }
        }

        public ExpressionType ExpressionType
        {
            get
            {
                return expressionType;
            }
            set
            {
                expressionType = value;
            }
        }

        public String HeaderName
        {
            get
            {
                return headerName;
            }
            set
            {
                headerName = value;
            }
        }

        public String ColumnName
        {
            get
            {
                return columnName;
            }
            set
            {
                columnName = value;
            }
        }

        public List<ReportRowSetting> ChildrenSettings
        {
            get
            {
                return childrenSettings;
            }
            set
            {
                childrenSettings = value;
            }
        }






        public IReportRowSetting GetParent()
        {
            return parent;
        }

        public void SetParent(IReportRowSetting setting)
        {
            this.parent = setting;
        }

        public IReportRowSetting[] GetChildrenSettings()
        {
            return childrenSettings.ToArray();
            //List<IReportRowSetting> list = new List<IReportRowSetting>();
            //foreach (ReportRowSetting setting in childrenSettings)
            //    list.Add(setting);
            //return list;
        }

        public IReportRowSetting CreateChildrenSetting(ExpressionType type, string headername, string columnname)
        {
            ReportRowSetting setting = new ReportRowSetting();
            setting.settingCode = StringHelper.GetSettingCode();
            setting.RowIndex = this.RowIndex;
            setting.columnName = columnname;
            setting.expressionType = type;
            setting.parent = this;
            setting.headerName = headername;
            this.childrenSettings.Add(setting);
            return setting;
        }

        public IReportRowSetting RemoveChildSetting(string settingCode)
        {
            ReportRowSetting remove = null;

            foreach (ReportRowSetting setting in childrenSettings)
            {
                if (!string.Equals(setting.SettingCode, settingCode, StringComparison.OrdinalIgnoreCase))
                    continue;

                remove = setting;
                break;
            }

            if (remove != null)
                childrenSettings.Remove(remove);

            return remove;
        }

        public IReportRowSetting GetChildren(string settingCode)
        {
            return GetChildren(this, settingCode);
        }

        public IReportRowSetting GetChildren(IReportRowSetting setting, string settingCode)
        {
            if (string.Equals(settingCode, setting.SettingCode))
                return setting;

            foreach (IReportRowSetting subsetting in setting.GetChildrenSettings())
            {
                IReportRowSetting rvalue = GetChildren(subsetting, settingCode);

                if (rvalue != null)
                    return rvalue;
            }

            return null;
        }

        public void Build()
        {
            foreach (IReportRowSetting child in childrenSettings)
            {
                (child as ReportRowSetting).SetParent(child);
                child.Build();
            }
        }

    }
}
